﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchoolLibrary.Utils;

namespace SchoolLibrary.Entity.Person
{
    public class Person : Identity
    {
        private const char SPACE_SUMBOL = ' ';
        private const char DOT_SUMBOL = '.';

        public const string NAME_COLUMN_NAME = "name";
        public const string MIDDELE_NAME_COLUMN_NAME = "middle_name";
        public const string SURNAME_COLUMN_NAME = "surname";

        public const string SAVE = "INSERT INTO :tableName (name, middle_name, surname) " +
            "VALUES(:name, :middleName, :surname)";
        public const string UPDATE = "UPDATE :tableName SET name = :name, middle_name = :middleName, " + 
            "surname = :surname where id = :id";

        private string name;
        private string middleName;
        private string surname;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }   

        public string MiddleName
        {
            get { return middleName; }
            set { middleName = value; }
        }

        public string Surname
        {
            get { return surname; }
            set { surname = value; }
        }

        public string FullName
        {
            get
            {
                return name + SPACE_SUMBOL +
                  middleName + SPACE_SUMBOL + surname;
            }
        }

        public string AbbreviatedName
        {
            get
            {
                StringBuilder abbreviatedName = new StringBuilder();
                abbreviatedName.Append(surname);
                abbreviatedName.Append(SPACE_SUMBOL);
                if (!StringUtil.isEmptyString(name))
                {
                    abbreviatedName.Append(name[0]);
                    abbreviatedName.Append(DOT_SUMBOL);
                    if (!StringUtil.isEmptyString(middleName))
                    {
                        abbreviatedName.Append(middleName[0]);
                        abbreviatedName.Append(DOT_SUMBOL);
                    }
                }
                return abbreviatedName.ToString();
            }
        }

        public override string ToString()
        {
            return AbbreviatedName;
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            if (obj == this)
            {
                return true;
            }

            if (!obj.GetType().Equals(this.GetType()))
            {
                return false;
            }

            Person person = (Person) obj;

            return (name != null ? name.Equals(person.Name) : person.Name == null) &&
                (middleName != null ? middleName.Equals(person.MiddleName) : person.MiddleName == null) &&
                (surname != null ? surname.Equals(person.Surname) : person.Surname == null);
        }
    }
}
